/************************************************************************
*
*  LibTLib
*  Copyright (C) 2010  Thor Qin
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
*
* Author: Thor Qin
* Bug Report: thor.qin@gmail.com
*
**************************************************************************/

#ifndef SYNTAX_H_
#define SYNTAX_H_

#include "../lex/lexical.h"

namespace tlib
{
namespace syn
{


template <typename T>
class Syntax: public Object
{
public:
	typedef RefPtr<Syntax> SyntaxPtr;
private:
	explicit Syntax();
public:
	~Syntax();
	static SyntaxPtr create(const std::basic_string<T>& syntax)
			throw(std::runtime_error);
	static SyntaxPtr create_by_file(const std::string& filename)
			throw(std::runtime_error);
	static SyntaxPtr create_by_bc(const void* data, unsigned long data_len)
			throw(std::runtime_error);
	static SyntaxPtr create_by_static_bc(const void* data, unsigned long data_len)
			throw(std::runtime_error);
	static SyntaxPtr create_by_bc_file(const std::string& filename)
			throw(std::runtime_error);

	void save_bc(const std::string& filename) throw(std::runtime_error);

	void parse(std::basic_istream<T>& input_stream) throw(std::runtime_error);
	bool best_match;
private:
	static SyntaxPtr create_by_stream(std::basic_istream<T>& in)
		throw(std::runtime_error);
private:
	void* _data;
	bool _allocated;
	typename lex::Lexical<T>::LexicalPtr _lex;
};




}
}

#endif /* SYNTAX_H_ */
